<template>
  <div class="video-box">
    <div class="video-box-main">
      <div class="video-box-close" @click="closeThis()"></div>
      <contentTitle :title="props.videoTitle"/>
      <video
          style="width: 800px;margin-top: 1vh"
          ref="videoPlayer"
          controls
      ></video>
      <button class="hide" @click="playOrPause">{{ isPlaying ? 'Pause' : 'Play' }}</button>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue';
import ContentTitle from "./contentTitle.vue";
import flvjs from 'flv.js';

const emit = defineEmits(['closeVideo']);
const props = defineProps({
  videoSource: String,
  videoTitle : String
})

const videoPlayer = ref(null);
const isPlaying = ref(false);
const volume = ref(0.5);

const closeThis = function (){
  emit("closeVideo");
}

const playOrPause = () => {
  if (videoPlayer.value.paused) {
    videoPlayer.value.play();
  } else {
    videoPlayer.value.pause();
  }
};

let flvPlayer = null;
onMounted(() => {
  if (flvjs.isSupported()) {
    flvPlayer = flvjs.createPlayer({
      type: 'flv',
      url: props.videoSource  // 替换为你的视频 URL
    });
    flvPlayer.attachMediaElement(videoPlayer.value);
    flvPlayer.load();

    //挂载时设置初始音量
    videoPlayer.value.volume = volume.value;

    flvPlayer.play();
  }else{
    console.log("error")
  }

  flvPlayer.on(flvjs.Events.ERROR, (errorType, errorDetail) => {
    console.error('FLV.js Error:', errorType, errorDetail);
  });

  // 挂载时设置初始音量
  // videoPlayer.value.volume = volume.value;

});

onBeforeUnmount(() => {
  if (flvPlayer !== null) {
    flvPlayer.destroy();
    flvPlayer = null;
  }
  // 组件销毁前清理资源
  videoPlayer.value.pause();
});

</script>
<style scoped>
.hide{
  display: none;
}
.video-box{
  position: absolute;
  display: flex;
  width: 100%;
  flex-wrap: wrap;
  justify-content: center;
  top: 19vh;
  .video-box-main{
    position: relative;
    background: rgba(43, 60, 77, 0.92);
    width: 820px;
    padding: 10px;
    .video-box-close{
      position: absolute;
      background: url("../../assets/img/public/headerModule/quit.png") no-repeat;
      height: 27px;
      width: 27px;
      right: 1vh;
      cursor: pointer;
      z-index: 1;
    }
  }
}
</style>